package com.yxp423.ssmempl.controller;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxp423.ssmempl.bean.Dept;
import com.yxp423.ssmempl.bean.User;
import com.yxp423.ssmempl.service.IDeptService;
import com.yxp423.ssmempl.service.IFuncService;
import com.yxp423.ssmempl.service.IRoleService;
import com.yxp423.ssmempl.service.IUserService;
import com.yxp423.ssmempl.utils.base.AjaxResult;
import com.yxp423.ssmempl.utils.consts.UserConst;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description: 公共拦截器，如获取当前用户，获取审核人列表
 * @Author: yxp
 * @CreateDate: 2019-05-04 12:32
 * @Version: 1.0
 */
@Controller
@RequestMapping("/const")
@Api(tags = "公共Controller")
public class ConstController {

	@Autowired
	private IUserService userService;
	@Autowired
	private IFuncService funcService;

	@Autowired
	private IDeptService deptService;

	@Autowired
	IRoleService roleService;

	Cache<String, List> allUserCache = CacheUtil.newLRUCache(10, DateUnit.MINUTE.getMillis() * 5);
	Cache<String, List> deptCache = CacheUtil.newLRUCache(10, DateUnit.MINUTE.getMillis() * 5);


	public void cacheClean(){
		allUserCache.clear();
		deptCache.clear();
	}

	/**
	 * 获得当前登录用户
	 *
	 * @return
	 */
	@GetMapping("/getCurrentUsername")
	@ResponseBody
	@ApiOperation("获得当前登录用户")
	public AjaxResult getCurrentUsername() {

		AjaxResult result = new AjaxResult();
		User user = (User) SecurityUtils.getSubject().getPrincipal();
//		String name = user.getName();
		if (ObjectUtil.isNull(user)) {
			result.setSuccess(false);
			result.setMsg("未登录");
			return result;
		}
		result.setSuccess(true);
		result.setData(user);

		return result;
	}


	/**
	 * 获得项目经理
	 *
	 * @return
	 */
	@GetMapping("/getReviewer")
	@ResponseBody
	@ApiOperation("获得项目经理")
	public AjaxResult getReviewer() {

		AjaxResult result = new AjaxResult();

		List<User> userList = userService.getReviewer(UserConst.USER_ROLE_PROJECTMANAGE);
		if (userList != null) {

			result.setSuccess(true);
			result.setData(userList);
		}

		return result;
	}


	/**
	 * 获得部门经理
	 *
	 * @return
	 */
	@GetMapping("/getDeptManager")
	@ResponseBody
	@ApiOperation("获得部门经理")
	public AjaxResult getDeptManager() {

		AjaxResult result = new AjaxResult();

		List<User> userList = userService.getReviewer(UserConst.USER_ROLE_DEPTMANAGE);
		if (userList != null) {

			result.setSuccess(true);
			result.setData(userList);
		}
		return result;
	}

	/**
	 * 加载菜单树
	 *
	 * @return
	 */
	@GetMapping("/funcTree")
	@ResponseBody
	@ApiOperation("加载菜单树")
	public AjaxResult funcTree() {

		AjaxResult result = funcService.findTree();

		return result;

	}


	@GetMapping("/getalluser")
	@ResponseBody
	@ApiOperation("获得所有的员工")
	public AjaxResult getAllUser() {

		AjaxResult result = new AjaxResult();

		try {

			if (ObjectUtil.isEmpty(allUserCache)) {

				List<User> allUser = userService.getAllUser();
				result.setSuccess(true);
				result.setData(allUser);

				allUserCache.put("allUser", allUser);

			} else {

				result.setSuccess(true);
				result.setData(allUserCache.get("allUser"));

			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg("获取员工列表失败" + e.toString());
		}

		return result;
	}

	@GetMapping("/getdept")
	@ResponseBody
	public AjaxResult getDept() {
		AjaxResult result = new AjaxResult();

		try {

			List list = deptCache.get("deptlist");
			if (ObjectUtil.isEmpty(list)) {


				List<Dept> deptList = deptService.getDepartment();
				if (ObjectUtil.isEmpty(deptList)) {

					result.setSuccess(false);
					result.setMsg("数据为空");
					return result;

				} else {

					result.setSuccess(true);
					result.setMsg("共有" + deptList.size() + "条数据");
					result.setData(deptList);
					deptCache.put("deptlist",deptList);
					return result;

				}
			} else {

				result.setSuccess(true);
				result.setMsg("共有" + list.size() + "条数据");
				result.setData(list);

			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg("错误，错误信息为" + e.toString());
		}

		return result;
	}

	@GetMapping("/getallroles")
	@ResponseBody
	public AjaxResult getRoles(){

		return roleService.getRole();
	}

	@GetMapping("/getRolesByUser")
	@ResponseBody
	public AjaxResult getRolesByUser(User user){



		return roleService.getRoleByUser(user);
	}

}
